﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Sfaf
{
    public partial class GenerateStepsFiles : Form
    {
        private RichTextBox richTextGivens = new RichTextBox();
        private RichTextBox richTextThens = new RichTextBox();
        private RichTextBox richTextWhens = new RichTextBox();
        public GenerateStepsFiles()
        {
            InitializeComponent();
        }

        private void btnGenerateStepFile_Click(object sender, EventArgs e)
        {
            try
            {
                
                richTextSteps.Clear();
                richTextGivens.Clear();
                richTextThens.Clear();
                richTextWhens.Clear();
                
                string[] richTextBoxLines = this.richTextFeature.Lines;
                string lineBefore = "";
                string lineAfter = "";

                richTextGivens.Text += Properties.Strings.GivenRegion+"\n";
                richTextWhens.Text += Properties.Strings.WhenRegion + "\n";
                richTextThens.Text += Properties.Strings.ThenRegion + "\n";
                foreach (string line in richTextBoxLines)
                {

                    

                    if (line.Contains(Properties.Strings.Given)) {
                        CreateGivenMethod(line);
                    }
                    if (line.Contains(Properties.Strings.When))
                    {
                        if (lineBefore.Contains(Properties.Strings.And))
                        {
                            CreateAndMethod(lineBefore, Properties.Strings.Given, ref richTextGivens);
                        }
                        CreateWhenMethod(line);
                    }
                    if(line.Contains(Properties.Strings.Then))
                    {
                        if (lineBefore.Contains(Properties.Strings.And))
                        {
                            CreateAndMethod(lineBefore, Properties.Strings.When, ref richTextWhens);
                        }
                        CreateThenMethod(line);   
                    }

                    if (line.Contains(Properties.Strings.And) && lineBefore.Contains(Properties.Strings.Then))
                    {
                        CreateAndMethod(line, Properties.Strings.Then,ref richTextThens);
                    }

                    lineBefore = line;
                                    
                }

               
                
          

                richTextGivens.Text += Properties.Strings.EndRegion + "\n";
                richTextWhens.Text += Properties.Strings.EndRegion + "\n";
                richTextThens.Text += Properties.Strings.EndRegion + "\n";


                richTextSteps.Text += richTextGivens.Text +"\n";
                richTextSteps.Text += richTextWhens.Text + "\n";
                richTextSteps.Text += richTextThens.Text + "\n";


                richTextSteps.Copy();
                MessageBox.Show("The step file is already your Clipboard");

               

            }
            catch(Exception error){
                Console.Write(error);
            }
        }



        private void CreateAndMethod(string line, string StartingNameLine, ref RichTextBox richTextToAddText)
        {

            string GivenComment = "[" + StartingNameLine + "(@\"" + line.Replace(StartingNameLine, "") + "\")]";
            GivenComment = GivenComment.Replace("And", "");
            //Convert Lower case words to CultureInfo  Example: print report ########## Print Report
            var lineToUpperCase = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(line);
            lineToUpperCase = lineToUpperCase.Replace("And", "");
            string GivenMethod = "[public void " + StartingNameLine + "" + lineToUpperCase.Replace(" ", "") + "(){ \n\n }";
            //

            richTextToAddText.Text += GivenComment + "\n";
            richTextToAddText.Text += GivenMethod + "\n";
        }

        private void CreateGivenMethod(string line)
        {
            
            string GivenComment = "[Given(@\""+line.Replace(Properties.Strings.Given, "") + "\")]";

            //Convert Lower case words to CultureInfo  Example: print report ########## Print Report
            var lineToUpperCase = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(line);
            string GivenMethod = "[public void " + lineToUpperCase.Replace(" ", "") + "(){ \n\n }";
            //

            richTextGivens.Text += GivenComment + "\n";
            richTextGivens.Text += GivenMethod + "\n";
        }
        private void CreateWhenMethod(string line)
        {
            string GivenComment = "[When(@\"" + line.Replace(Properties.Strings.When, "") + "\")]";


            //Convert Lower case words to CultureInfo  Example: print report ########## Print Report
            var lineToUpperCase = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(line);
            string GivenMethod = "[public void " + lineToUpperCase.Replace(" ", "") + "(){ \n\n }";
            //

            richTextWhens.Text += GivenComment + "\n";
            richTextWhens.Text += GivenMethod + "\n";
        }
        private void CreateThenMethod(string line)
        {
            string GivenComment = "[Then(@\"" + line.Replace(Properties.Strings.Then, "") + "\")]";

            //Convert Lower case words to CultureInfo  Example: print report ########## Print Report
            var lineToUpperCase = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(line);
            string GivenMethod = "[public void " + lineToUpperCase.Replace(" ", "") + "(){ \n\n }";
            //

            richTextThens.Text += GivenComment + "\n";
            richTextThens.Text += GivenMethod + "\n";
        }


        private void richTextFeature_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
